<?php
/*
 * Created on May 12, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *  User_group Model Class
 *
 */
class  Comment_model extends CI_Model 
{
	/**
	 * @var unknown_type
	 */
	var $id  ;  
	/**
	 * @var unknown_type
	 */
	var $fullname  ;  
	/**
	 * @var unknown_type
	 */
	var $company  ; 
	/**
	 * @var unknown_type
	 */
	var $address  ;  
	/**
	 * @var unknown_type
	 */
	var $phone;
	/**
	 * @var unknown_type
	 */
	var $fax;
	/**
	 * @var unknown_type
	 */
	var $email;
	/**
	 * @var unknown_type
	 */
	var $title;
	/**
	 * @var unknown_type
	 */
	var $content;
	/*
	 * @access	public
	 */
	 var $recei_date;
	/*
	 * @access	public
	 */
	 var $answer_date;
	/*
	 * @access	public
	 */
	 var $status;
	/*
	 * @access	public
	 */
	 
function __construct() {
        parent::__construct();
    }
    
		
	function insert($data){
		$sql = "INSERT INTO gtb_comment (news_id, content, fullname, email, address, phone, date_create, active)
				VALUES(".
				$this->db->escape($data['news_id']).", " .
				$this->db->escape($data['content']).", " .
				$this->db->escape($data['fullname']).", " .
				$this->db->escape($data['email']).", " .
				$this->db->escape($data['address']).", " .
				$this->db->escape($data['phone']).", " .
				$this->db->escape(date('Y-m-d H:i:s', time())).", " .
				1 
				.")";
		$this->db->query($sql);
		return $this->db->insert_id(); 
	}
	
		
	function update($data){
		 $sql = "UPDATE gtb_comment 
				SET ".
				" content = " . $this->db->escape($data['content'])  . "," .
		 		" active = " . intval($data['active'])  . "," .
				" date_update = " .  $this->db->escape($data['date_update']) .
				" WHERE id = " .intval($data['id']);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
		
	function delete($id){
		if(!is_array($id)) $id = array($id);
		$ids = implode(',', $id);
		$sql = "DELETE FROM gtb_comment WHERE id IN ($ids)";
		//$sql = "DELETE FROM gtb_pcategory WHERE id_pcategory = " . intval($id);
		$this->db->query($sql);
		return $this->db->affected_rows(); 
	}
	
	function getAllNewsId($news_id){
		$result = null;
		
		$sql = "SELECT * FROM gtb_comment 
		WHERE news_id = " . intval($news_id) . ' AND active = 1';
		
		$query = $this->db->query($sql);
		
		$result = $query->result_array();
       	
		return $result;
	}
	
	function getAll( $offset = 0, $number_record = 10, $active = -1, $news_id = 0){
		$active = intval($active);
		$news_id = intval($news_id);
		
		$sql = "SELECT `id`, `news_id`, `content`, `fullname`, `email`, `address`, `phone`, `date_create`, `date_update`, `active`
				FROM gtb_comment 
				WHERE (news_id = $news_id OR 0 = $news_id) AND (active = $active OR -1 = $active)
				ORDER BY id DESC LIMIT $offset, $number_record";
		
		//echo $sql;
		$query = $this->db->query($sql);
		$result = $query->result_array();
       	
		return $result;
	}
	
	function countAll($active, $news_id){
		$result = null;
		$active = intval($active);
		$news_id = intval($news_id);
		$sql = "SELECT COUNT(*) as num_record 
				FROM gtb_comment 
				WHERE WHERE (news_id = $news_id OR 0 = $news_id) AND (active = $active OR -1 = $active)";
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result[0]->num_record;
	}
	
	function getByParentCode($code_parent){
		$result = null;
		$sql = "SELECT *
				FROM gtb_pcategory WHERE code_parent = ".$this->db->escape($code_parent)." ORDER BY id_pcategory DESC";
		
		$query = $this->db->query($sql);
		$result = $query->result();
       	
		return $result;
	}
	
	function getMaxPosition($category_parent_id){
		
		$sql = "SELECT MAX(position) as position_max
				FROM category 
				WHERE category_parent_id = $category_parent_id";
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0]->position_max;
		}
       
		return 0;
	}
	
	function getById($id){
		
		$sql = "SELECT `id`, `news_id`, `content`, `fullname`, `email`, `address`, `phone`, `date_create`, `date_update`, `active`
				FROM gtb_comment
				WHERE id = " . intval($id);
		$query = $this->db->query($sql);
		if($result = $query->result_array()){
			return $result[0];
		}
       
		return null;
	}
	
	
//	
}

// EOF

